package io.adrainty.algorithm.leetcode.lcr;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * LCR 015. 找到字符串中所有字母异位词
 * @link <a href="https://leetcode.cn/problems/VabMRr/description/">...</a>
 * @author AdRainty
 * @version V1.0.0
 * @since 2024/9/8 下午10:17
 */
@SuppressWarnings("unused")
public class LCR015 {

    private LCR015() {}

    public static List<Integer> findAnagrams(String s, String p) {
        if (s.length() < p.length()) return List.of();

        int[] cnt1 = new int[26];
        int[] cnt2 = new int[26];

        for (int i = 0; i < p.length(); i++) {
            cnt1[s.charAt(i) - 'a']++;
            cnt2[p.charAt(i) - 'a']++;
        }

        List<Integer> ans = new ArrayList<>();
        if (Arrays.equals(cnt1, cnt2)) ans.add(0);
        for (int i = 0; i < s.length() - p.length(); i++) {
            cnt1[s.charAt(i) - 'a']--;
            cnt1[s.charAt(i + p.length()) - 'a']++;
            if (Arrays.equals(cnt1, cnt2)) ans.add(i + 1);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(findAnagrams("cbaebabacd", "abc"));
        System.out.println(findAnagrams("abab", "ab"));
        System.out.println(findAnagrams("baa", "aa"));
    }

}
